package dingding.使用线程池;

import java.util.concurrent.*;

public class AsyncFutureExample {
    private final static int AVALABLE_PROCESSORS = Runtime.getRuntime().availableProcessors();

    private final static ThreadPoolExecutor pool = new ThreadPoolExecutor(AVALABLE_PROCESSORS,
            AVALABLE_PROCESSORS * 2, 1, TimeUnit.MINUTES,
            new LinkedBlockingDeque<>(5),
            new ThreadPoolExecutor.CallerRunsPolicy());

    public static String doSomethingA(){
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("-----doSomethingA");
        return "A Task Done";
    }

    public static String doSomethingB(){
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("-----doSomethingB");
        return "B Task Done";
    }


    public static void main(String[] args) throws ExecutionException, InterruptedException {
        Long start = System.currentTimeMillis();

        Future<String> future = pool.submit(() -> {
            return doSomethingA();
        });




        Long end = System.currentTimeMillis();

        System.out.println(future.get());
        System.out.println(end - start);
    }
}
